# -*- coding:UTF-8 -*-
import os,sys
import cv2
import random
import time
import numpy as np
import config 
def get_screen():

    #截屏口令
    cmd_get = config.rootPath+'/adb shell screencap -p /sdcard/'+config.tmpImg
    #发送图片口令
    cmd_send = config.rootPath+'/adb pull sdcard/'+config.tmpImg+' '+config.tmpPath
    #截屏和发送操作
    os.system(cmd_get)
    os.system(cmd_send)
    img = cv2.imread(config.tmpPath+config.tmpImg,0)
    # img=cv2.cvtColor( cv2.imread(config.tmpPath+config.tmpImg,0), cv2.COLOR_BGR2GRAY)
    return img
def getSingleImageData(path,type='dev',is_gray=0):
     t=''
     if type=='dev':
        t=config.devPath+path
     elif type=='img':
        t=config.imgPath+path
     else:
        t=config.tmpPath+path   
     print(t)    
     if is_gray==0:
        return cv2.imread(t,0)
     else:    
        return cv2.cvtColor( cv2.imread(t), cv2.COLOR_BGR2GRAY)

def matchPos(img1, template):
    """img1代表待匹配图像, img2代表模板"""
    res = cv2.matchTemplate(img1,template,cv2.TM_CCOEFF_NORMED)
    maxres = res.max()
    if maxres>0.8:
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
        return [min_val,max_val,min_loc,max_loc]
    else:
        return 0
def match(img1, template):
    """img1代表待匹配图像, img2代表模板"""
    res = cv2.matchTemplate(img1,template,cv2.TM_CCOEFF_NORMED)
    maxres = res.max()
    if maxres>0.8:
        return 1
    else:
        return 0

def get_randxy(x, y):
    """产生一个在x,y二维区域内的随机位置,x,y为两个元素的列表，变量范围"""
    xc = random.randint(x[0], x[1])
    yc = random.randint(y[0], y[1])
    return xc,yc

def get_randtime(a, b):
    """产生a,b间的随机时间延迟"""
    time.sleep(random.uniform(a, b))

def click(x, y):
    """输入两个二维列表，表示要点击的位置的x坐标，y坐标"""
    cmd_click = config.rootPath+'/adb shell input tap {} {}'.format(x, y)
    os.system(cmd_click)   
def swipe(x1=0,y1=0,x2=0,y2=0):
    if x1==0:
       x1=340
       y1=400
       x2=370
       y2=202 
    cmd_swipe=config.rootPath+'/adb shell input swipe  '+str(x1)+' '+str(y1)+' '+str(x2)+' '+str(y2)
    os.system(cmd_swipe) 